<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Using STL Containers with Eigen</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TopicStlContainers.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Using STL Containers with Eigen<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a> &raquo; <a class="el" href="group__DenseMatrixManipulation__Alignement.html">Alignment issues</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<h1><a class="anchor" id="StlContainers_summary"></a>
Executive summary</h1>
<p>If you're compiling in <span class="cpp17">[c++17]</span> mode only with a sufficiently recent compiler (e.g., GCC&gt;=7, clang&gt;=5, MSVC&gt;=19.12), then everything is taken care by the compiler and you can stop reading.</p>
<p>Otherwise, using STL containers on <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a>, or classes having members of such types, requires the use of an over-aligned allocator. That is, an allocator capable of allocating buffers with 16, 32, or even 64 bytes alignment. Eigen does provide one ready for use: <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with types requiring a non-standard alignment.">aligned_allocator</a>.</p>
<p>Prior to <span class="cpp11">[c++11]</span>, if you want to use the <code>std::vector</code> container, then you also have to <code> #include &lt;Eigen/StdVector&gt; </code>.</p>
<p>These issues arise only with <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a> and <a class="el" href="group__TopicStructHavingEigenMembers.html">structures having such Eigen objects as member</a>. For other Eigen types, such as Vector3f or MatrixXd, no special care is needed when using STL containers.</p>
<h1><a class="anchor" id="allocator"></a>
Using an aligned allocator</h1>
<p>STL containers take an optional template parameter, the allocator type. When using STL containers on <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a>, you need tell the container to use an allocator that will always allocate memory at 16-byte-aligned (or more) locations. Fortunately, Eigen does provide such an allocator: <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with types requiring a non-standard alignment.">Eigen::aligned_allocator</a>.</p>
<p>For example, instead of </p><div class="fragment"><div class="line">std::map&lt;int, Eigen::Vector4d&gt;</div>
</div><!-- fragment --><p> you need to use </p><div class="fragment"><div class="line">std::map&lt;int, Eigen::Vector4d, std::less&lt;int&gt;, </div>
<div class="line">         <a class="code" href="classEigen_1_1aligned__allocator.html">Eigen::aligned_allocator&lt;std::pair&lt;const int, Eigen::Vector4d&gt;</a> &gt; &gt;</div>
<div class="ttc" id="aclassEigen_1_1aligned__allocator_html"><div class="ttname"><a href="classEigen_1_1aligned__allocator.html">Eigen::aligned_allocator</a></div><div class="ttdoc">STL compatible allocator to use with types requiring a non-standard alignment.</div><div class="ttdef"><b>Definition:</b> Memory.h:873</div></div>
</div><!-- fragment --><p> Note that the third parameter <code>std::less&lt;int&gt;</code> is just the default value, but we have to include it because we want to specify the fourth parameter, which is the allocator type.</p>
<h1><a class="anchor" id="StlContainers_vector"></a>
The case of std::vector</h1>
<p>This section is for c++98/03 users only. <span class="cpp11">[c++11]</span> (or above) users can stop reading here.</p>
<p>So in c++98/03, the situation with <code>std::vector</code> is more complicated because of a bug in the standard (explanation below). To workaround the issue, we had to specialize it for the <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with types requiring a non-standard alignment.">Eigen::aligned_allocator</a> type. In practice you <b>must</b> use the <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with types requiring a non-standard alignment.">Eigen::aligned_allocator</a> (not another aligned allocator), <b>and</b> #include &lt;Eigen/StdVector&gt;.</p>
<p>Here is an example: </p><div class="fragment"><div class="line"><span class="preprocessor">#include&lt;Eigen/StdVector&gt;</span></div>
<div class="line"><span class="comment">/* ... */</span></div>
<div class="line">std::vector&lt;Eigen::Vector4f,Eigen::aligned_allocator&lt;Eigen::Vector4f&gt; &gt;</div>
</div><!-- fragment --><p><span class="note"><b>Explanation:</b> The <code>resize()</code> method of <code>std::vector</code> takes a <code>value_type</code> argument (defaulting to <code>value_type()</code>). So with <code>std::vector&lt;<a class="el" href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b" title="4×1 vector of type double.">Eigen::Vector4d</a>&gt;</code>, some <a class="el" href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b" title="4×1 vector of type double.">Eigen::Vector4d</a> objects will be passed by value, which discards any alignment modifiers, so a <a class="el" href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b" title="4×1 vector of type double.">Eigen::Vector4d</a> can be created at an unaligned location. In order to avoid that, the only solution we saw was to specialize <code>std::vector</code> to make it work on a slight modification of, here, <a class="el" href="group__matrixtypedefs.html#ga9b2fcb53776a2829871f8a49009bef0b" title="4×1 vector of type double.">Eigen::Vector4d</a>, that is able to deal properly with this situation. </span></p>
<h2><a class="anchor" id="vector_spec"></a>
An alternative - specializing std::vector for Eigen types</h2>
<p>As an alternative to the recommended approach described above, you have the option to specialize std::vector for <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> types requiring alignment. The advantage is that you won't need to declare std::vector all over with <a class="el" href="classEigen_1_1aligned__allocator.html" title="STL compatible allocator to use with types requiring a non-standard alignment.">Eigen::aligned_allocator</a>. One drawback on the other hand side is that the specialization needs to be defined before all code pieces in which e.g. <code>std::vector&lt;Vector2d&gt;</code> is used. Otherwise, without knowing the specialization the compiler will compile that particular instance with the default <code>std::allocator</code> and you program is most likely to crash.</p>
<p>Here is an example: </p><div class="fragment"><div class="line"><span class="preprocessor">#include&lt;Eigen/StdVector&gt;</span></div>
<div class="line"><span class="comment">/* ... */</span></div>
<div class="line">EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(<a class="code" href="group__matrixtypedefs.html#ga3b934095f8a2834e6cc27267427239d3">Matrix2d</a>)</div>
<div class="line">std::vector&lt;Eigen::Vector2d&gt;</div>
<div class="ttc" id="agroup__matrixtypedefs_html_ga3b934095f8a2834e6cc27267427239d3"><div class="ttname"><a href="group__matrixtypedefs.html#ga3b934095f8a2834e6cc27267427239d3">Eigen::Matrix2d</a></div><div class="ttdeci">Matrix&lt; double, 2, 2 &gt; Matrix2d</div><div class="ttdoc">2×2 matrix of type double.</div><div class="ttdef"><b>Definition:</b> Matrix.h:501</div></div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
